<?php 
	namespace Home\Controller;
	use Think\Controller;

	class ProvinceController extends AuthController
	{

		// public function getState($code)
		// {
		// 	$s = array(
		// 		"-1"=>"账号或密码错误",
		// 		"0" =>"登录成功",
		// 		"1" =>"账号失效",
		// 		"2" =>"最近操作时间更新失败",
		// 		"3" =>"退出成功",
		// 		"4" =>"没有目标地址访问权限",
		// 		"5" =>"用户未登录",
		// 		"6" =>"没有可访问的地址",
		// 		"7" =>"节目信息获取成功",
		// 		"8" =>"登录界面",
		// 		"9" =>"省份信息获取成功",
		// 		"10" =>"删除省份成功",
		// 		"11" =>"删除省份失败",
		// 		"12" =>"菜单信息获取成功"
		// 	);
		// 	return array("code"=>intval($code),"message"=>strval($s[$code]));
		// }

		public function all()
		{
			if(IS_POST){
				$page = I("post.page",1);
				$rows = I("post.rows",10);
				$res = D("Province")->get_all_province($page,$rows);
				$output['result'] = $this->getState("9");
				$output['total'] = $res['total']; 
				$output['rows'] = $res['rows']; 
				echo json_encode($output);
			}else{
				$page = I("post.page",1);
				$rows = I("post.rows",10);
				$res = D("Province")->get_all_province($page,$rows);
				$output['result'] = $this->getState("9");
				$output['total'] = $res['total']; 
				$output['rows'] = $res['rows']; 
				echo json_encode($output);
			}
		}	

		public function delete()
		{
			if(IS_POST){
				$ids = I("post.ids",'');
				$province_ids = explode(",", $ids);
				$res = D("Province")->delete_province($province_ids);
				if($res){
					$output['result'] = $this->getState("10"); 
					echo json_encode($output);
				}else{
					$output['result'] = $this->getState("11"); 
					echo json_encode($output);
				}
			}else{
				
			}
		}

		public function add()
		{
			if(IS_POST){
				$province = I("post.province","");
				$status = I("post.status","");
				$malv = I("post.malv","");
				if($province){
					$res = D("Province")->add_province($province,$status,$malv);
				}
				if($res){
					$output = 1; 				
					echo json_encode($output);
				}else{
					$output = 0; 				
					echo json_encode($output);
				}
			}else{
				$province = I("post.province","");
				$status = I("post.status","");
				if(($province && $status) != ""){
					$res = D("Province")->add_province($province,$status);
				}
				if($res){
					$output = 1; 				
					echo json_encode($output);
				}else{
					$output = 0; 				
					echo json_encode($output);
				}
			}
		}

		public function edit_select()
		{
			if(IS_POST){
				$id = I("post.id","");
				$id = intval($id);
				$res = D("Province")->edit_select($id);
				// $output['provinceData'] = $res;
				echo json_encode($res);
			}else{
				$id = I("post.id","1");
				$id = intval($id);
				$res = D("Province")->edit_select($id);
				//dump($res);exit();
				// $output['provinceData'] = $res;
				// $output['provinceData'] = $res;
				echo json_encode($res);
			}
		}

		public function edit()
		{
			if(IS_POST){
				$id = I("post.id","");
				$status = I("post.status","");
				$province = I("post.province","");	
				$malv = I("post.malv","");
				$id = intval($id);
				$res = D("Province")->edit_pro($id,$status,$province,$malv);
				if($res){
					$res1 = 1;
				}else{
					$res1 = 0;
				}
				echo json_encode($res1);
			}else{
				$id = I("post.id","1");
				$status = I("post.status","1");
				$province = I("post.province","0211010");		
				$id = intval($id);
				$res = D("Province")->edit_pro($id,$status,$province);
				if($res){
					$res1 = 1;
				}else{
					$res1 = 0;
				}
				echo json_encode($res1);
			}
		}
	}
 ?>